import json

from devicetest.api import Asserts
from devicetest.aw.OpenHarmony import CommonOH, WifiHelper
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup, common_teardown
from aw.pom import Setting


class PreTest_RK3568_Basic_Setting_0800(TestCase, Setting):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 1.进入桌面，点击设置-WLAN，开启WLAN
    # 2.可用WLAN列表中点击wifi进行连接
    # 3.点击已连接的wifi，点击删除按钮
    # 预期结果：
    # 1.WLAN按钮被点亮，展示可用WLAN列表
    # 2.输入正确密码，wifi连接成功，最上层有显示wifi图标
    # 3.点击连接的wifi有弹窗提示，点击删除后则断开连接，最上层图标也会消失

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)
        Step('预置条件2：关闭wifi')
        try:
            WifiHelper.closeWifi(self.Phone1)
        except:
            pass

    def process(self):
        Step('步骤1：点击设置-WLAN，开启WLAN')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.checkIfTextExist(self.Phone1, '设置')
        CommonOH.touchByText(self.Phone1, 'WLAN')
        CommonOH.touchByType(self.Phone1, 'Toggle')
        Step('预期结果1：WLAN按钮被点亮，展示可用WLAN列表')
        property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
        Asserts.assert_true(property_dict.get('checked'))
        CommonOH.checkIfTextExist(self.Phone1, '可用WLAN')

    def teardown(self):
        common_teardown(self.Phone1, self.bundle_name)
        Step('收尾2：关闭wifi')
        try:
            WifiHelper.closeWifi(self.Phone1)
        except:
            pass
